package dbserver;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Hashtable;

public class DBDataReader implements DataReader {
	protected Properties properties;
	
	public DBDataReader(){
		properties = null;
	}
	public DBDataReader(Properties props){
		this.properties = props;
	}
	@Override
	public Record findRecord(String termToFind)
	{	
		Connection con = null;
		Record rec = null;
		try{
			con = DriverManager.getConnection(properties.getProperty("db_url"),
												properties.getProperty("login"),
												properties.getProperty("password"));
			Statement sqlStmt = con.createStatement();
			ResultSet resultSet = sqlStmt.executeQuery("SELECT * FROM dictionary WHERE term =" + "'" + termToFind + "'");
			while(resultSet.next()){
				rec = new Record(resultSet.getInt(1),
										resultSet.getString(2),
										resultSet.getString(3));
			}
			con.close();
		}
		catch (Exception e){
			e.printStackTrace();
		}
		return rec;
	}
}
